//
//  FFPayWayView.m
//  FFMall
//
//  Created by ale tan on 2017/10/20.
//  Copyright © 2017年 IT.Bear. All rights reserved.
//

#import "FFPayTypeView.h"
#import "FFPayTypeCell.h"

#define Row_Height 60

@interface FFPayTypeView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong)FBOrderModel *orderModel;
@property (nonatomic ,strong)ChoosePayType payTypeBlock;
@property (nonatomic ,strong) FBPayTypeModel *choosePayType;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *listHeight;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *toBottom;

@end

@implementation FFPayTypeView

+ (instancetype)payTypeViewModel:(FBOrderModel *)orderModel ChoosePayType:(ChoosePayType)payType{
    FFPayTypeView *payTypeView = [[[NSBundle mainBundle] loadNibNamed:@"FFPayTypeView" owner:nil options:nil] firstObject];
    payTypeView.orderModel = orderModel;
    payTypeView.payTypeBlock = payType;
    payTypeView.choosePayType = orderModel.payMethods[0];
    payTypeView.listHeight.constant = orderModel.payMethods.count*Row_Height;
    return payTypeView;
}

- (void)show{
    UIView *view = [UIApplication sharedApplication].keyWindow.rootViewController.view;//[[UIApplication sharedApplication].delegate window];
    self.frame = view.bounds;
    [self layoutIfNeeded];
    [view addSubview:self];
}

- (void)awakeFromNib{
    [super awakeFromNib];
    
    _toBottom.constant = isDeviceIphoneX?34:0;
    
    [self setTableView];
}

- (void)setTableView{
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = RGB_VIEW_BG_GRAY;
    if (isIOS11) {
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
        }
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
    }
}

#pragma mark -UITableView-
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _orderModel.payMethods.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return Row_Height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FFPayTypeCell *payCell = [tableView dequeueReusableCellWithIdentifier:@"FFPayTypeCell"];
    if (!payCell) {
        payCell = [[[NSBundle mainBundle] loadNibNamed:@"FFPayTypeCell" owner:nil options:nil] firstObject];
    }
    FBPayTypeModel *payModel = _orderModel.payMethods[indexPath.row];
    if ([payModel.payMethodId integerValue] == 1 || [payModel.payMethodId integerValue] == 4) {
        //支付宝
        payCell.payImageView.image = [UIImage imageNamed:@"order_details-zhifubao"];
        payCell.payTypeName.text = @"支付宝支付";
        payCell.payTipsView.text = @"大额支付推荐使用";
    }
    if ([payModel.payMethodId integerValue] == 2) {
        //微信
        payCell.payImageView.image = [UIImage imageNamed:@"order_details-weixing"];
        payCell.payTypeName.text = @"微信支付";
        payCell.payTipsView.text = @"方便快捷，可支持银行卡支付";
    }
    
    //选中
    if ([_choosePayType.payMethodId isEqual:payModel.payMethodId]) {
        UIImage *select = [UIImage imageNamed:@"selected"];
        payCell.selectImage.image = select;
    }
    else{
        UIImage *select = [UIImage imageNamed:@"unSelected"];
        payCell.selectImage.image = select;
    }
    //        }
    return payCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    _choosePayType = _orderModel.payMethods[indexPath.row];
    [_tableView reloadData];
}

#pragma mark -Events-
- (IBAction)clickToCanclePay:(UIButton *)sender {
//    UIAlertView *alertView = [[UIAlertView alloc]initWithOtherTitle:@"确认放弃支付？" message:@"超过订单支付时效后，订单将被取消，请尽快完成支付" cancelBtnTitle:@"继续支付" otherBtnTitle:@"放弃" clickIndexBlock:^(NSInteger clickIndex) {
//        if (clickIndex) {
            if (_payTypeBlock) {
                _payTypeBlock(nil);
            }
            [self removeFromSuperview];
//        }
//    }];
//    [alertView showFFAlertView];
}
- (IBAction)clickToConfirmPay:(id)sender {
    if (_payTypeBlock) {
        _payTypeBlock(_choosePayType);
    }
}

@end
